//
//  WDDElectronicBankView.m
//  SZBank
//
//  Created by rimi on 16/7/19.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "WDDElectronicBankView.h"
#define MYCOLOR [UIColor colorWithRed:0.227 green:0.349 blue:0.078 alpha:1.000]
@interface WDDElectronicBankView()<ButtonAndTableViewDelegate>
{
    CGFloat frameWidth;/**  自身宽度 */
    CGFloat frameHeight;/**  自身高度 */
    NSMutableDictionary *sourceDic;
}

@property (nonatomic,strong) UIImageView *bgImageView;/**  背景图 */

@property (nonatomic,strong) UIScrollView *scrollView;/**  滚动视图 */
@end
@implementation WDDElectronicBankView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        frameWidth = self.frame.size.width;
        frameHeight = self.frame.size.height;
        [self addSubview:self.bgImageView];
        sourceDic = [NSMutableDictionary dictionary];
        NSMutableArray *nameArray = [NSMutableArray array];/**  产品名称 */
        NSMutableArray *conceptArray = [NSMutableArray array];/**  产品概念 */
        NSMutableArray *featureArray = [NSMutableArray array];/**  产品特点 */
        NSMutableArray *conditionArray = [NSMutableArray array];/**  受理条件 */
        /**  电子银行数据请求 */
        [NetWorkingDataManager getProtocolTemplateUpdateWithProtocolid:@"402886c240a553640140aa6271680042" success:^(id sender){
            for (NSDictionary *dic1 in sender) {
                NSDictionary *dic2 = [dic1 objectForKey:@"protocolData"];
                [nameArray addObject:[dic2 objectForKey:@"产品名称"][0]];
                [conceptArray addObject:[dic2 objectForKey:@"产品概念"][0]];
                [featureArray addObject:[dic2 objectForKey:@"产品特点"][0]];
                [conditionArray addObject:[dic2 objectForKey:@"受理条件"][0]];
            }
            [sourceDic setObject:nameArray[0] forKey:@"title"];
            [sourceDic setObject:nameArray forKey:@"list"];
            [sourceDic setObject:conceptArray forKey:@"产品概念"];
            [sourceDic setObject:featureArray forKey:@"产品特点"];
            [sourceDic setObject:conditionArray forKey:@"受理条件"];
            [self initUserInterface];
        } failure:nil];
    }
    return self;
}

- (void)initUserInterface{
    ButtonAndTableView *btView = [[ButtonAndTableView alloc] initWithFrame:CGRectMake(frameWidth * 0.02, frameHeight * 0.01, frameWidth * 0.96, frameHeight * 0.07) dataSource:sourceDic backImageName:@"储蓄存款_01" selectImageName:@"储蓄存款_01"];
    btView.delegate = self;
    [self addSubview:btView];
    [self addSubview:[self getScrollView:0]];
}

- (void)btnTableView:(ButtonAndTableView *)btnTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath selectTitle:(NSString *)selectTitle{
    [self.scrollView removeFromSuperview];
    [self addSubview:[self getScrollView:indexPath.row]];
}

- (UIScrollView *)getScrollView:(NSInteger)index{
    /**  产品名称 */
    UILabel *nameTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, frameHeight * 0.08, frameWidth * 0.1, frameHeight * 0.07)];
    [self getLabel:nameTitle textFont:[UIFont systemFontOfSize:26] textColor:MYCOLOR contentText:@"产品名称"];
    NSString *string1 = [sourceDic objectForKey:@"list"][index];
    CGFloat height1 = [NSString textString:string1 fontSize:24 maxWidth:frameWidth * 0.93].height;
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(frameWidth * 0.03, CGRectGetMaxY(nameTitle.frame), frameWidth * 0.93, height1)];
    [self getLabel:name textFont:[UIFont systemFontOfSize:24] textColor:[UIColor blackColor] contentText:string1];
    /**  产品概念 */
    UILabel *introduceTitle = [[UILabel alloc] initWithFrame:CGRectMake(0,  CGRectGetMaxY(name.frame), frameWidth * 0.1, frameHeight * 0.07)];
    [self getLabel:introduceTitle textFont:[UIFont systemFontOfSize:26] textColor:MYCOLOR contentText:@"产品概念"];
    NSString *string2 = [sourceDic objectForKey:@"产品概念"][index];
    CGFloat height2 = [NSString textString:string2 fontSize:24 maxWidth:frameWidth * 0.93].height;
    UILabel *introduce = [[UILabel alloc] initWithFrame:CGRectMake(frameWidth * 0.03, CGRectGetMaxY(introduceTitle.frame), frameWidth * 0.93, height2)];
    [self getLabel:introduce textFont:[UIFont systemFontOfSize:24] textColor:[UIColor blackColor] contentText:string2];
    /**  产品特点 */
    UILabel *featureTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(introduce.frame), frameWidth * 0.1, frameHeight * 0.07)];
    [self getLabel:featureTitle textFont:[UIFont systemFontOfSize:26] textColor:MYCOLOR contentText:@"产品特点"];
    NSString *string3 = [sourceDic objectForKey:@"产品特点"][index];
    CGFloat height3 = [NSString textString:string3 fontSize:24 maxWidth:frameWidth * 0.93].height;
    UILabel *feature = [[UILabel alloc] initWithFrame:CGRectMake(frameWidth * 0.03, CGRectGetMaxY(featureTitle.frame), frameWidth * 0.93, height3)];
    [self getLabel:feature textFont:[UIFont systemFontOfSize:24] textColor:[UIColor blackColor] contentText:string3];
    /**  受理条件 */
    UILabel *conditionTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(feature.frame), frameWidth * 0.1, frameHeight * 0.07)];
    [self getLabel:conditionTitle textFont:[UIFont systemFontOfSize:26] textColor:MYCOLOR contentText:@"受理条件"];
    NSString *string4 = [sourceDic objectForKey:@"受理条件"][index];
    CGFloat height4 = [NSString textString:string4 fontSize:24 maxWidth:frameWidth * 0.93].height;
    UILabel *condition = [[UILabel alloc] initWithFrame:CGRectMake(frameWidth * 0.03, CGRectGetMaxY(conditionTitle.frame), frameWidth * 0.93, height4)];
    [self getLabel:condition textFont:[UIFont systemFontOfSize:24] textColor:[UIColor blackColor] contentText:string4];
    /**  滚动视图 */
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(frameWidth * 0.03, frameHeight * 0.1, frameWidth * 0.93, frameHeight * 0.88)];
    CGFloat totalHeight = height1 + height2 + height3 + height4 + 450;
    self.scrollView.contentSize = CGSizeMake(frameWidth * 0.03, totalHeight);
    self.scrollView.backgroundColor = [UIColor clearColor];
    
    [self.scrollView addSubview:nameTitle];
    [self.scrollView addSubview:name];
    [self.scrollView addSubview:introduceTitle];
    [self.scrollView addSubview:introduce];
    [self.scrollView addSubview:featureTitle];
    [self.scrollView addSubview:feature];
    [self.scrollView addSubview:conditionTitle];
    [self.scrollView addSubview:condition];
    return self.scrollView;
}

- (void)getLabel:(UILabel *)label textFont:(UIFont *)textFont textColor:(UIColor *)textColor contentText:(NSString *)contentText{
    label.font = textFont;
    label.textColor = textColor;
    label.text = contentText;
    label.numberOfLines = 0;
}

- (UIImageView *)bgImageView{
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frameWidth, frameHeight)];
        _bgImageView.image = [LocalDataManager getImageWithSourceOfPath:@"通用背景"];
    }
    return _bgImageView;
}
@end
